﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Nop.Admin.Extensions;
using Nop.Admin.Models.Customers;
using Nop.Admin.Models.Shops;
using Nop.Core.Domain.Customers;
using Nop.Core.Domain.Shops;
using Nop.Services.Customers;
using Nop.Services.Security;
using Nop.Services.Shops;

namespace Nop.Admin.Controllers
{
    public class ShopController :BaseNopController 
    {
        private readonly IPermissionService _permissionService;
        private readonly IShopService _shopService;
        private readonly ICustomerService _customerService;

        public ShopController(IPermissionService permissionService, IShopService shopService, ICustomerService customerService)
        {
            _permissionService = permissionService;
            _shopService = shopService;
            _customerService = customerService;
        }

        public ActionResult Create()
        {
            //if (!_permissionService.Authorize(StandardPermissionProvider.ManageShops))
            //    return AccessDeniedView();

            var notUsedShopModerators = _customerService.GetAllNotUsedShopModerators();
            var notUserShopModeratorModels = notUsedShopModerators.Select(c=>c.ToModel()).ToList();

            var shopModel = new ShopModel()
                                {
                                    ShopName = string.Empty,
                                    NotUsedShopModerators = notUserShopModeratorModels,
                                    SelectedShopModerator = notUserShopModeratorModels==null || notUserShopModeratorModels.Count==0? null: notUserShopModeratorModels[0]
                                };

            return View("Create",shopModel);
        }

        [HttpPost]
        public JsonResult Create(ShopModel model)
        {
            //if (!_permissionService.Authorize(StandardPermissionProvider.ManageShops))
            //    return AccessDeniedView();


            var selectedShopModerator = model.SelectedShopModerator;

            try
            {
                _shopService.CreateShopAndModerator(model.ShopName, selectedShopModerator.Id);
                return Json(new { Status  = "Success"});
            }
            catch(Exception e)
            {
                
                return Json(new { Status  = "Failed", Message = e.Message});
            }

        }



        public ActionResult List()
        {
            //if (!_permissionService.Authorize(StandardPermissionProvider.ManageShops))
            //    return AccessDeniedView();

            var shops = _shopService.GetAllShops();


            var shopListModel = new ShopListModel()
                                    {
                                        Shops = shops.Select(PrepareShopModelForList).ToList()
                                    };

            return View("List",shopListModel);
        }


        [NonAction]
        protected ShopModel PrepareShopModelForList(Shop shop)
        {
            return new ShopModel()
            {
                ShopId = shop.Id,
                ShopName = shop.ShopName,
                Owner = PrepareCustomerModel(shop.Owner)
            };
        }

        [NonAction]
        protected CustomerModel PrepareCustomerModel(Customer owner)
        {
            return new CustomerModel()
                       {
                           Id = owner.Id,
                           Username = owner.Username,
                       };
        }

    }
}
